# ! /usr/bin/nev python
# -*-coding:utf-8*-
#author:Big Big
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import re
path = 'C:/Users/82606/Desktop/'

#电影评论数，评分，片长箱形图
def boxplot(df):
    df = df[['人数','评分','片长']]
    plt.style.use('ggplot')
    colnm = df.columns.tolist()
    fig = plt.figure(figsize=(12, 6))
    for i in range(3):
        plt.subplot(1, 3, i + 1)
        sns.boxplot(df[colnm[i]], orient="v", width=0.5, color='r')
        plt.ylabel('people', fontsize=12)
    plt.tight_layout()
    print('\nFigure 1: Univariate Boxplots')
    plt.savefig(path + "box.png", figsize=[12, 6])
    plt.show()


#电影评论数量,评分，片长的可视化
def each_plt(df):
    df = df[['人数', '评分', '片长']]
    colnm = df.columns.tolist()
    plt.figure(figsize=(12, 8))
    for i in range(3):
        plt.subplot(1, 3, i + 1)
        df[colnm[i]].hist(bins=100, color='b')
        plt.xlabel(colnm[i], fontsize=12)
        plt.ylabel('Frequency')
    plt.tight_layout()
    plt.savefig(fname="peoplemarktime.png", figsize=[10, 10])
    plt.show()
    print('\nFigure 2: Univariate Histograms')


#top250电影年份分布图
def year_plt(df):
    year_pd = df['年份']
    year = []
    for i in year_pd.values:
        year.append(i[0:4])
    year = list(map(int,year))
    #便利每部电影的年份,分三档3〉成熟期（1927—1945）。4〉发展期（1945—80年代末）。5〉电影新时期（90年代—）。
    date1 = []
    date2 = []
    date3 = []
    for i in year:
        if i <= 1945:
            date1.append(i)
        elif 1945< i < 1990:
            date2.append(i)
        else:
            date3.append(i)
    x = ['-1945','1945-1990','1990-']
    data = [len(date1),len(date2),len(date3)]
    plt.bar(x, data, color='b', width=0.25, label="amount")
    plt.savefig(fname="time.png", figsize=[10, 10])
    plt.show()
    print('\nFigure 3: Univariate Histograms')


#电影类型分布
def type_plt(df):
    plt.figure(figsize=(15, 8))
    alltypes = []
    type_pd = df['类型']
    #数据清洗
    for i in list(type_pd.values):
        typenames = re.findall('[\u4e00-\u9fa5]+',i[1:-1])
        for type in typenames:
            if type not in alltypes:
                alltypes.append(type)
    #获取到所有类型后翻译成英文
    alltypes_english=['drama','crime', 'love', 'gay' ,'disaster','action', 'comedy', 'war', 'animated', 'fantasy', 'history', 'science', 'mystery', 'adventure', 'music', 'dancing', 'thriller', 'ancient costume', 'family', 'biography', 'Sport ',' Western ', 'Erotica ',' Children ', 'Documentary ',' Martial Arts ', 'Horror ']
    #存放每个类型的电影数量
    counts = []
    for k in range(len(alltypes)):
        counts.append(0)
    for i in list(type_pd.values):
        typenames = re.findall('[\u4e00-\u9fa5]+', i[1:-1])
        for type in typenames:
            index = alltypes.index(type)
            counts[index] += 1
    #绘图
    x = alltypes_english
    data = counts
    plt.bar(x, data, color='b', width=0.25, label="amount")
    plt.savefig(fname="type.png", figsize=[10, 10])
    plt.show()
    print('\nFigure 4: Univariate Histograms')

#国籍分布
def get_allcountry_message(df):
    country_pd = df['国籍']
    allcountry = []
    # 数据清洗
    for i in list(country_pd.values):
        countrynames = re.findall('[\u4e00-\u9fa5]+', i)
        for country in countrynames:
            if country not in allcountry:
                allcountry.append(country)
    # 存放每个类型的电影数量
    count = []
    for k in range(len(allcountry)):
        count.append(0)
    for i in list(country_pd.values):
        countrynames = re.findall('[\u4e00-\u9fa5]+', i)
        for country in countrynames:
            index = allcountry.index(country)
            count[index] += 1
    return allcountry, count

def country_plt(df):
    allcountry,count = get_allcountry_message(df)
    allcountry_english = [' us', 'China', 'Hong Kong', 'Mexico', 'Australia', 'Canada', 'French', 'Italian', 'Japan', 'the', 'India', 'Switzerland', 'German', 'Korea', 'New Zealand', 'Lebanon','Cyprus', 'Qatar', 'Taiwan', 'polish', 'Spain', 'Iran', 'Denmark', 'Sweden', 'Brazil', 'Austria', 'South Africa', 'Greece', 'Argentina', 'Thailand', 'Ireland,','Hungary', 'Jordan', 'the Czech', 'the united Arab emirates', 'Monaco ',' Belgium ']
    label = allcountry_english
    count_average = list(map(lambda a:a/sum(count),count))
    plt.pie(count_average, labels=label)
    plt.savefig(fname="country.png", figsize=[10, 10])
    plt.show()
    print('\nFigure 5: Univariate Histograms')








